package com.walkline.app;

import java.util.Vector;

import net.rim.device.api.io.transport.TransportInfo;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;

public class RadioAppConfig
{
	public static final String APP_TITLE = "iRadio";
	public static final String UNDERLINE = "\u0332";
	public static final String BBW_APPID = "58645271";
	//public static final String SKU = "0xceb3388f932aac65L";
	public static final String STRINGS = "IRadioztbj";

	public static final Background bgColor_Gradient=BackgroundFactory.createLinearGradientBackground(Color.GRAY, Color.GRAY, Color.BLACK, Color.BLACK);
	public static final Background bg_popup_Transparent=BackgroundFactory.createSolidTransparentBackground(Color.BLACK, 200);

	public static int[] preferredTransportTypes = {
													TransportInfo.TRANSPORT_TCP_WIFI,
													TransportInfo.TRANSPORT_BIS_B,
													TransportInfo.TRANSPORT_TCP_CELLULAR,
													TransportInfo.TRANSPORT_WAP2
												  };
	public static int[] disallowedTransportTypes = {
													TransportInfo.TRANSPORT_MDS,
													TransportInfo.TRANSPORT_WAP
												   };

	public static final Font FONT_MAIN_TITLE = Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight(Ui.UNITS_pt) + 1, Ui.UNITS_pt);

	private static Vector _data;
	private static PersistentObject _store;
	private Vector _elements;

	private static final int REGISTER_STATUS = 0;

	private boolean _register_status = false;

	public RadioAppConfig()
	{
		_elements = new Vector(1);

		for (int i=0; i<_elements.capacity(); i++)
		{
			_elements.addElement(new Object());
		}
	}

	private void setElement(int id, Object value) {_elements.setElementAt(value, id);}

	private boolean getElementBoolean(int id)
	{
		boolean result = false;
		Object valueObject = _elements.elementAt(id); 

		if (valueObject instanceof Boolean)
		{
			result = ((Boolean) valueObject).booleanValue();
		}

		return result;
	}

	public void setRegisterStatus(boolean value)
	{
		setElement(REGISTER_STATUS, new Boolean(value));
		_register_status = value;
	}

	public boolean getResisterStatus() {return _register_status;}

	public void initialize()
	{
		synchronized(_store)
		{
			try {
				_data = (Vector) _store.getContents();

				if (!_data.isEmpty())
				{
					_elements = (Vector) _data.lastElement();

					setRegisterStatus(getElementBoolean(REGISTER_STATUS));
				} else{
					makeDefaultSettings();
				}
			} catch (Exception e) {
				_store.setContents(new Vector());
				_store.forceCommit();
				_data = new Vector();

				makeDefaultSettings();
			}
		}
	}

	public void save()
	{
		setRegisterStatus(getResisterStatus());

		_data.addElement(_elements);

		synchronized(_store)
		{
			_store.setContents(_data);
			_store.commit();
		}
	}

	private void makeDefaultSettings()
	{
		setRegisterStatus(false);

		_data.addElement(_elements);

		synchronized(_store)
		{
			_store.setContents(_data);
			_store.forceCommit();
		}
	}

	static {_store = PersistentStore.getPersistentObject(0xceb3388f932aac65L);}

	//SKU: 0xceb3388f932aac65L (blackberry_web_radio_written_by_walkline_wang)
}